#!/usr/bin/env python
# -*- coding: utf-8 -*-
# --------------------------------------------------------
# Descripttion:
# version: 0.0.1
# Author: Wenwei Fu (2516800961@qq.com)
# FilePath: /fastapi_byd/app/api/routes/expert.py
# Create: 2021-01-04 16:37:40
# LastAuthor: Wenwei Fu
# lastTime: 2021-01-10 15:01:42
# --------------------------------------------------------

import uuid
import datetime
from typing import Optional
from pydantic import BaseModel
from fastapi import APIRouter, Request, Body, Depends, HTTPException
from app.api.dependencies.authentication import get_current_user_authorizer
from app.db.schemas import Traffic_Lane_Code # 道路编码
from app.db.database import get_db

router = APIRouter()

@router.get("/list", name="查询类型列表", dependencies=[Depends(get_db)])
async def lane_code_list(
    pageNum: Optional[int] = 1,
    pageSize: Optional[int] = 10,
    request: Request=None
):
    query = Traffic_Lane_Code.select().order_by(Traffic_Lane_Code.lane_value).paginate(pageNum, pageSize).dicts()
    results = [item for item in query]
    count = Traffic_Lane_Code.select().count()
    return {
        "code": 200,
        "message": "查询成功",
        "data": {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "total": count,
            "records": results
        }
    }

@router.get("/dicts", name="查询类型 字典", dependencies=[Depends(get_db)])
async def lane_code__dicts(
    request: Request=None
):
    query = Traffic_Lane_Code.select().order_by(Traffic_Lane_Code.lane_value).dicts()
    results = [item for item in query]
    return {
        "code": 200,
        "message": "查询成功",
        "data": results
    }

@router.post("/create", name="创建类型", dependencies=[Depends(get_db), Depends(get_current_user_authorizer())])
async def create_lane_code_(
    lane_value: str,
    lane_label: str,
    created_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    updated_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    request: Request = None
):
    results = {}
    if lane_value is not None:
        results["lane_value"] = lane_value
    if lane_label is not None:
        results["lane_label"] = lane_label
    Traffic_Lane_Code.create(**results)
    print('创建类型', results)

    return {
        "code": 200,
        "message": "创建成功",
        "data": results
    }

@router.put("/update", name="更新类型", dependencies=[Depends(get_db), Depends(get_current_user_authorizer())])
async def update_lane_code_(
    id: str,
    lane_value: str,
    lane_label: str,
    request: Request = None
):
    updatesql = (Traffic_Lane_Code.update(
                    lane_label=lane_label,
                    lane_value=lane_value,
                    updated_at=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                ).where(Traffic_Lane_Code.id == id)
                .execute())
    if updatesql == 1:
        return {
                "code": 200,
                "message": "更新成功",
                "data": {
                    "id": id,
                    "lane_label": lane_label
                }
            }
    else:
        return {
                "code": 201,
                "message": "更新失败",
                "data": {
                    "id": id,
                    "lane_label": lane_label
                }
            }

@router.delete("/delete", name="删除类型", dependencies=[Depends(get_db), Depends(get_current_user_authorizer())])
async def delete_lane_code(
    id: str,
    request: Request=None
):
    query = Traffic_Lane_Code.delete().where(Traffic_Lane_Code.id == id).execute()
    if query == 1:
        return {
            "code": 200,
            "message": "删除成功",
            "data": {
                "id": id
            }
        }
    else:
        return {
            "code": 200,
            "message": "删除失败",
            "data": {
                "id": id
            }
        }